# 错误
import traceback


def invoke_error():
    if 1 == 1:
        print("always")


# 异常处理
class BException(Exception):  # 继承Exception基类
    pass


class CException(BException):
    pass


class DException(CException):
    pass


def invoke_exception():
    for cls in [BException, CException, DException]:
        try:
            raise cls()
        except DException:
            print("D")
        except CException:
            print("C")
        except BException:
            print("B")


def invoke_without_type():
    for _ in [BException, CException, DException]:
        try:
            raise BException()
        except DException:
            print("D")
        except:
            print("处理其他全部异常")


def invoke_exception_param():
    try:
        x = 1 / 0
    except ZeroDivisionError as err:
        print("Exception")
        print(err.args)
        print(err)


def diyException(level):
    if  level > 0:
        raise Exception("raise exception", level)
        print("我是不会执行的")


def invoke_raise():
    try:
        diyException(2)
    except Exception as err:
        print(err)
        traceback.print_exc()
    # except 'error level' as err:
    #     print(err)


# 自定义异常
class DiyError(RuntimeError):
    def __init__(self, arg):
        self.args = arg


def invoke_selfError():
    try:
        raise DiyError("my diy exception")
    except DiyError as e:
        print(e)



















